package com.csist.dao;

import com.csist.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class FriendDao {
    /**
     * 根据id查询其好友
     * @param id 用户id
     * @return 好友id(int)集合
     */
    public ArrayList<Integer> getFriendListById(int id){
        Connection conn = JDBCUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select f_id from friend where u_id = ? ";
        String sql1 = "select u_id from friend where f_id = ? ";
        ArrayList<Integer> friends = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while (rs.next()){
                friends.add(rs.getInt(1));
            }

            ps = conn.prepareStatement(sql1);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while (rs.next()){
                friends.add(rs.getInt(1));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(conn,ps,rs);
        }
        return friends;
    }

    /**
     * 添加好友
     * @param userID 用户id
     * @param friendId 好友id
     * @return true-->添加成功, false-->添加失败
     */
    public boolean addFriend(int userID, int friendId){
        if (canAddFriend(userID,friendId)){
            Connection conn = JDBCUtil.getConnection();
            PreparedStatement ps = null;
            String sql  = "insert into friend value(?,?)";
            boolean bl = false;
            try {
                ps = conn.prepareStatement(sql);
                ps.setInt(1,userID);
                ps.setInt(2,friendId);
                bl = ps.executeUpdate()==1?true:false;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                JDBCUtil.close(conn,ps);
            }
            return bl;
        }
        return false;
    }

    /**
     * 判断两个用户是否互为好友
     * @param userID 用户id
     * @param friendId 用户id
     * @return true-->是好友 false-->不是好友
     */
    public boolean canAddFriend(int userID, int friendId){
        Connection conn = JDBCUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select count(1) from friend where u_id = ? and f_id = ?";
        boolean bl = true;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,userID);
            ps.setInt(2,friendId);
            rs = ps.executeQuery();
            if (rs.next()){
                bl = false;
            }
            ps.setInt(1,friendId);
            ps.setInt(2,userID);
            rs = ps.executeQuery();
            if (rs.next()){
                bl = false;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(conn,ps,rs);
        }
        return bl;
    }
}
